/**
 * Filename: YNMusicWebActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/5/16 19:44
 */
package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.ShareUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * 音乐相册Web页
 */
public class YNMusicWebActivity extends BaseActivity implements ISkinUpdate {
    private Toolbar mToolbar;
    private WebView webView;
    private String code;
    private ProgressDialog mProgressDialog;
    private Tencent mTencent;
    private BaseUiListener listener;
    private String shareTitle;
    private String shareDesc;
    private String openUrl;
    private String atitle;
    private int agroupid;
    private int aeid;
    private String shareUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_music_web);
        BaseApplication.getInstance().mActivitys.add(this);
        mTencent = Tencent.createInstance(getString(R.string.QQ_APP_ID), this.getApplicationContext());
        openUrl = getIntent().getStringExtra(ACTIONs.aUrl);
        agroupid = getIntent().getIntExtra(ACTIONs.aGroupid, 0);
        aeid = getIntent().getIntExtra(ACTIONs.aEid, 0);
        atitle = getIntent().getStringExtra(ACTIONs.aTitle);
        listener = new BaseUiListener();
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.musicWeb_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        mToolbar.setTitle(atitle);

        webView = (WebView) findViewById(R.id.musicWebView);
        try {
            code = YNInviteSecret.encryptDES(String.valueOf(application.getCurID()), getString(R.string.UNKNOW_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        webView.loadUrl(openUrl);
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= 19) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        }

        mProgressDialog = ProgressDialog.show(this, "", CommonConstants.TIP_LOAD_DATA);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    mProgressDialog.dismiss();
                } else {
                    // 加载中
                    mProgressDialog.show();
                }
            }
        });

        String datas = "eid=" + aeid + ",userid=" + application.getCurID();
        shareTitle = "时光影集 作者-" + application.getCurUser().getUnickname();
        shareDesc = StringUtils.isEmptyString(atitle) ? "世界很大，而你在我身边" : atitle;
        try {
            code = YNInviteSecret.encryptDES(String.valueOf(datas), getString(R.string.UNKNOW_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        shareUrl = "http://www.zhuiyinanian.com/templetmusic/" + openUrl.substring(openUrl.lastIndexOf("/") + 1) + "?code=" + code;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_share_qq:
                ShareUtils.onInvateFromQQ(mTencent, YNMusicWebActivity.this, shareUrl, shareTitle, shareDesc, listener);
                break;
            case R.id.action_share_wx:
                ShareUtils.onInvateFromWx(application.getWxApi(), YNMusicWebActivity.this, shareUrl, shareTitle, shareDesc);
                break;
            case R.id.action_share_line:
                ShareUtils.onShareToTimeLine(application.getWxApi(), YNMusicWebActivity.this, shareUrl, shareTitle, shareDesc);
                break;
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, listener);
    }

    private class BaseUiListener implements IUiListener {
        protected void doComplete(JSONObject values) {
        }

        @Override
        public void onComplete(Object o) {
            showLog(o.toString());
            doComplete((JSONObject) o);
        }

        @Override
        public void onError(UiError e) {
        }

        @Override
        public void onCancel() {
        }
    }


    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
